package csu.rjxyg.action.userAction;

import java.util.ArrayList;
import java.util.List;

import csu.rjxyg.action.BaseAction;
import csu.rjxyg.bean.core.BaseBean;
import csu.rjxyg.bean.core.BaseList;
import csu.rjxyg.bean.core.BaseObject;
import csu.rjxyg.action.constant.UserConstant;
import csu.rjxyg.factory.core.BeanFactory;
import csu.rjxyg.factory.core.UtilFactory;
import csu.rjxyg.service.BaseUserService;
import csu.rjxyg.service.impl.UserService;
import csu.rjxyg.util.LogUtil;

public class GetUserInfoAction extends BaseAction {
	private LogUtil log;	
	private String userId;

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public GetUserInfoAction() {
		log = UtilFactory.getLogUtil(getClass());
	}

	public void getAllUserInfo() {
		String name = null;

		try {
			BaseUserService userSerivce = (BaseUserService) factory.getBean(BaseAction.USER_SERVICE);
			BaseObject object = (BaseObject) this.getSession().get(UserConstant.USER_SESSION);
			name = (String) object.get(UserConstant.USER_ID);

			BaseList temp = userSerivce.getAllBeanInfo();
			
			for(int i = 0;i < temp.size();i++){
				BaseObject temp1 = temp.get(i);
				temp1.set(UserConstant.USER_PASS, "");
			}
			
			//对权限进行清洗去除看不见的权限和用户
			temp = this.checkPermission(temp, (String) object.get(UserConstant.USER_LOGIN_QX));
			
			//将用户权限转化为中文
			temp = this.changePermissionName(temp);
			
			
			
			this.out(temp.getBean().toString());
			//将用户权限转化为系统识别
			temp = this.changePermissionName(temp);
			log.info(name + "查询所有用户信息");
		} catch (Exception e) {
			log.info(name + "查询所有用户信息失败");
			e.printStackTrace();
			log.info(e.getMessage());

		}
	}

	// public void getAllPermission(){
	// String name = null;
	// try{
	// List temp = user
	// this.getSession().put(ValueBean.PERMISSION_INFO, temp);
	// BaseObject object = (BaseObject)
	// this.getSession().get(ValueBean.USER_SESSION);
	// name = (String) object.get(ValueBean.USER_ID);
	// log.info( name + "查询所有用户信息");
	// }catch(Exception e){
	// log.info( name + "查询所有用户信息失败");
	// e.printStackTrace();
	// log.info(e.getMessage());
	//
	// }
	public void getUserInfo() {
		String name = null;
		try {
			BaseObject object = (BaseObject) this.getSession().get(UserConstant.USER_SESSION);
			name = (String) object.get(UserConstant.USER_NAME);
			String pass = (String) object.get(UserConstant.USER_PASS);
			object.set(UserConstant.USER_PASS, "");
			
			//将权限转成中文
			object = this.changePermissionName(object);
			
			this.out(object.getBean().toString());
			
			//将权限转成系统识别
			object = this.changePermissionName(object);
			//将用户密码复原存入session
			object.set(UserConstant.USER_PASS, pass);
			log.info(name + "查看个人信息成功");
		} catch (Exception e) {
			log.info(name + "查看个人信息失败");
			e.printStackTrace();
			log.info(e.getMessage());
		}
	}

	public void getUserInfoById() {
		String name = null;
		try {
			BaseUserService userSerivce = (BaseUserService) factory.getBean(BaseAction.USER_SERVICE);
			BaseObject object = (BaseObject) this.getSession().get(UserConstant.USER_SESSION);
			name = (String) object.get(UserConstant.USER_ID);
			
			BaseObject userInfo = userSerivce.getBeanInfoById(userId);
			if (userInfo == null) {
				this.out("error:不存在数据");
				return;
			}
			userInfo.set(UserConstant.USER_PASS, "");
			
			//将权限转成中文
			userInfo = this.changePermissionName(userInfo);
			
			this.out(userInfo.getBean().toString());
			
			//将权限转成系统识别
			userInfo = this.changePermissionName(userInfo);
			
			log.info(name + "查看个人信息成功");
			log.info(userInfo.getBean().toString());
		} catch (Exception e) {
			log.info(name + "查看个人信息失败");
			log.info(e.getMessage());
		}
	}
	
}
